package com.ming.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ming.common.CodeMsg;
import com.ming.common.Result;
import com.ming.common.config.file.FileService;
import com.ming.common.config.file.UploadProperties;
import com.ming.domain.Book;
import com.ming.from.BookEForm;
import com.ming.query.BookEQuery;
import com.ming.query.BookQuery;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ming.mapper.BookEMapper;
import com.ming.domain.BookE;
import com.ming.service.BookEService;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

/**
 *@Author:何元才
 *@Desc:
 *@data:2021-05-07 10:21
 */
@Service
public class BookEServiceImpl extends ServiceImpl<BookEMapper, BookE> implements BookEService{

    @Autowired
    private BookEMapper bookEMapper;

    @Autowired
    private FileService fileService;

    @Autowired
    private UploadProperties uploadProperties;
    /**
     * 分页查询
     *
     * @param query
     * @return
     */
    @Override
    public Result queryPage(BookEQuery query) {
        //开启分页查询
        Page<BookE> page = new Page<>(query.getPage(),query.getLimit());
        Page<BookE> bookEPage = bookEMapper.selectPage(page, new LambdaQueryWrapper<BookE>()
                .like(StringUtils.hasText(query.getEName()),BookE::getEName,query.getEName())
                .like(StringUtils.hasText(query.getAuthor()),BookE::getAuthor,query.getAuthor())
                .eq(!ObjectUtils.isEmpty(query.getType()),BookE::getType,query.getType())
                .eq(!ObjectUtils.isEmpty(query.getStatus()),BookE::getStatus,query.getStatus())
        );
        return new Result(bookEPage);
    }

    /**
     * 检查入库
     *
     * @param eId
     * @return
     */
    @Override
    public Result check(Integer eId) {
        //找到选择的图书
        BookE bookE = bookEMapper.selectById(eId);
        //检查图书状态 0 未检查入库的图书
        if (bookE.getStatus() != 0 ){
            return new Result(CodeMsg.BOOK_SAVE_ERROR);
        }
        bookE.setStatus(1);
        bookEMapper.updateById(bookE);
        return new Result();
    }

    /**
     * 新增电子书
     *
     * @param bookEForm
     * @return
     */
    @Override
    public Result add(BookEForm bookEForm ) {

        BookE bookE = new BookE();
        BeanUtils.copyProperties(bookEForm,bookE);
        bookE.setHot(0);
        bookE.setUrl(bookEForm.getFile());
        bookE.setStatus(0);
        bookE.setCreateTime(new Date());
        bookEMapper.insert(bookE);
        return new Result();
    }
}
